package com.jlt.web.comm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.jlt.entity.comm.Quartz;
import com.jlt.service.comm.QuartzManager;
import com.jlt.web.CrudActionSupport;
import com.jlt.web.util.Struts2ActionUtils;

/**
 * 定时任务管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数. 演示带分页的管理界面.
 * 
 * @author zhoutao
 */
@Namespace("/comm")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "quartz.action", type = "redirect") })
public class QuartzAction extends CrudActionSupport<Quartz> {

	private static final long serialVersionUID = 1L;
	private static final String ENTITY_NAME = "定时任务";
	@Autowired
	private QuartzManager quartzManager;

	// -- 页面属性 --//
	private String triggerName;
	private Quartz entity;
	private Page<Quartz> page = new Page<Quartz>(15);// 每页15条记录
	private List<Quartz> allQuartzList;

	public void setId(String triggerName) {
		this.triggerName = triggerName;
	}

	public Quartz getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		page = quartzManager.searchQuartz(page, filters);
		// allQuartzList = quartzManager.getAllQuartz();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	public String pause() throws Exception {
		quartzManager.pauseTrigger(triggerName);
		Map<String, String> message = new HashMap<String, String>();
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
		Struts2Utils.renderJson(message);
		return null;
	}

	public String start() throws Exception {
		quartzManager.resumeTrigger(triggerName);
		Map<String, String> message = new HashMap<String, String>();
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
		Struts2Utils.renderJson(message);
		return null;
	}

	public String stop() throws Exception {
		quartzManager.removeTrigdger(triggerName);
		Map<String, String> message = new HashMap<String, String>();
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
		Struts2Utils.renderJson(message);
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
	}

	public QuartzManager getQuartzManager() {
		return quartzManager;
	}

	public void setQuartzManager(QuartzManager quartzManager) {
		this.quartzManager = quartzManager;
	}

	public List<Quartz> getAllQuartzList() {
		return allQuartzList;
	}

	public void setAllQuartzList(List<Quartz> allQuartzList) {
		this.allQuartzList = allQuartzList;
	}

	// -- 页面属性访问函数 --//
	/**
	 * list页面显示用户分页列表.
	 */
	public Page<Quartz> getPage() {
		return page;
	}

}
